package wli.web.util;

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

public class MyFile  {
    DecimalFormat td = new DecimalFormat("0.00");
    private Vector vd = null;

    public void setDir (String dir) {
	File df = new File (dir);
	vd = new Vector();
	loadSubdir(df);
	File sd = null;

	for(int i=0; i<vd.size();i++) {
	    sd = (File)vd.elementAt(i);
	    loadSubdir(sd);
	}
    }

    private void loadSubdir(File f) {
	File index_f = null;
	if(f.isDirectory()) {
	    index_f = new File(f.getPath(),"index.html");
	    int n=0;
	    while(index_f.exists()) {
		n++;
		index_f.delete();
		if(n>5) {
		    break;
		}
	    }

	    File sf[] = f.listFiles();
	    for(int i=0; i<sf.length; i++) {
		if(sf[i].isDirectory()) {
		    if(!vd.contains(sf[i])) {
			vd.add(sf[i]);
		    }
		}
	    }
	}
    }

    public int getTotalDirs() {
	return vd.size();
    }

    public Vector getDirListV() {
	return vd;
    }

    public int getTotalFiles() {
	int tf = 0;
	Enumeration enuma = vd.elements();
	File f = null;
	File sf[] = null;

	while (enuma.hasMoreElements()) {
	    f = new File(""+enuma.nextElement());
	    sf = f.listFiles();
	    for(int n=0; n<sf.length;n++) {
		if(sf[n].isFile()) {
		    tf++;;
		}
	    }
	}
	return tf;
    }

    public String getTotalFileSize() {
	long ts = 0;
	Enumeration enumb = vd.elements();
	File f = null;
	File sf[] = null;

	while (enumb.hasMoreElements()) {
	    f = new File(""+enumb.nextElement());
	    sf = f.listFiles();
	    for(int n=0; n<sf.length;n++) {
		if(sf[n].isFile()) {
		    ts += sf[n].length();
		}
	    }
	}
	return td.format(ts/(1024*1000.00));
    }
}



